openInit();
var channel=null;
// 1. 获取支付通道
function plusReady(){
// 获取支付通道
	plus.payment.getChannels(function(channels){
		alipay=channels[0];
		weixin=channels[1];
	},function(e){
		alert("获取支付通道失败："+e.message);
	});
}

document.addEventListener('plusready',plusReady,false);
$('#pay').click(function(){
	var id=$('.payStyle li').find('.selected').attr('data-id');
	if(!id){
		alert('请选择商品或支付方式');
		return false;
	}
	pay(id);
})


var ALIPAYSERVER='http://www.wsjietubao.com/index/payment/alipay/total/';
var WXPAYSERVER='http://www.wsjietubao.com/index/payment/wxpay/total/';
// 2. 发起支付请求
function pay(id){
	// 从服务器请求支付订单
	var PAYSERVER='';
	var total=$('.hover').attr('data-id');
	if(id=='alipay'){
		PAYSERVER=ALIPAYSERVER+total;
		var channel= alipay;
	}else if(id=='wxpay'){
		PAYSERVER=WXPAYSERVER+total;
		var channel= weixin;
	}else{
		plus.nativeUI.alert("不支持此支付通道！",null,"捐赠");
		return;
	}
	var xhr=new XMLHttpRequest();
	xhr.onreadystatechange=function(){
	switch(xhr.readyState){
		case 4:
		if(xhr.status==200){

			plus.payment.request(channel,xhr.responseText,function(result){

				plus.nativeUI.alert("支付成功！",function(){
					vip();
				});
				},function(error){
					plus.nativeUI.alert("支付失败：" + error.code);
			});
		}else{
			alert("获取订单信息失败！");
		}
		break;
		default:

		break;
	}
	}
	xhr.open('GET',PAYSERVER);
	xhr.send();
}

function vip(){
	var num=$('.hover').attr('data-num');
	jQuery.support.cors = true;
    		var httpHost = getHttpHost();
    		var token=getToken();
    		$.post(httpHost+'/index/Base/openVip.html',{token:token,num:num},function(d){

		      	window.localStorage.setItem('token',d);

		    })
}
